package com.course.service;

import com.course.dao.StudentDAO;
import com.course.entity.Student;
import java.util.List;

public class StudentService {
    private final StudentDAO studentDAO;
    
    public StudentService() {
        this.studentDAO = new StudentDAO();
    }
    
    // 学生注册
    public boolean register(Student student) {
        // 检查用户名是否已存在
        if (studentDAO.findByUsername(student.getUsername()) != null) {
            return false;
        }
        
        // 检查学号是否已存在
//        if (studentDAO.findByStudentNumber(student.getStudentNumber()) != null) {
//            return false;
//        }
        
        // 添加学生
        return studentDAO.addStudent(student);
    }
    
    // 学生登录
    public Student login(String username, String password) {
        Student student = studentDAO.findByUsername(username);
        if (student != null && student.getPassword().equals(password)) {
            return student;
        }
        return null;
    }
    
    // 更新学生信息
    public boolean updateStudent(Student student) {
        return studentDAO.updateStudent(student);
    }
    
    // 修改密码
    public boolean changePassword(int studentId, String oldPassword, String newPassword) {
        Student student = studentDAO.findByStudentNumber(String.valueOf(studentId));
        if (student != null && student.getPassword().equals(oldPassword)) {
            return studentDAO.updatePassword(studentId, newPassword);
        }
        return false;
    }
    
    // 根据学号查找学生
    public Student findByStudentNumber(String studentNumber) {
        return studentDAO.findByStudentNumber(studentNumber);
    }

    // 根据ID查找学生
    public Student findById(int id) {
        return studentDAO.findById(id);
    }

    // 删除学生
    public boolean deleteStudent(int id) {
        return studentDAO.deleteStudent(id);
    }

    // 获取所有学生列表（不包括密码）
    public List<Student> getAllStudents() {
        return studentDAO.getAllStudents();
    }

    // 根据学号、姓名、班级搜索学生
    public List<Student> searchStudents(String studentId, String studentName, String className) {
        return studentDAO.searchStudents(studentId, studentName, className);
    }
} 